package com.xhwl.data.controller;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.common.enums.config.IndustryEnum;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Industry;
import com.xhwl.common.pojo.cent.sdata.devplatform.Developer;
import com.xhwl.data.dao.devplatform.IDeveloperDao;
import com.xhwl.data.pojo.vo.IndustryVO;
import com.xhwl.data.service.IIndustryService;
import com.xhwl.data.util.EnumListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author JCccc
 * @Description
 * @Date 2021/7/20 17:01
 */
@RestController
public class IndustryController {

    @Autowired
    private IIndustryService industryService;

    @Autowired
    private IDeveloperDao developerDao;

    /**
     * 业态类型选择器
     *
     * @return
     */
    @GetMapping(value = "/industry/list")
    public ResultJson getIndustryList(@RequestHeader(value = "appId", required = true) String appId,
                                       @RequestHeader(value = "clientType", required = false) Integer clientType,
                                       @RequestHeader(value = "industryId", required = true) Short industryId) {
        //List<Map<String, Object>> industryInfos = EnumListUtil.enumToListMap(IndustryEnum.class);
        List<Map<String, Object>> result = new ArrayList<>();
        List<Industry> list = industryService.getAll();
        if(CollectionUtil.isNotEmpty(list)){
            for(Industry vo:list){
                Map<String, Object> map= new HashMap<>();
                map.put("id",vo.getId());
                map.put("description",vo.getName());
                result.add(map);
            }
        }
        return ResultJson.success(result);
    }

    /**
     * 分页查询
     * @param industryVO
     * @param developer
     * @return
     */
    @PostMapping("/industry/page")
    public ResultJson page(@RequestBody IndustryVO industryVO, Developer developer){
        Developer developerDb = developerDao.selectById(developer.getId());
        if (null == developerDb) {
            return ResultJson.fail("开发者不存在");
        }
        QueryWrapper<Industry> industryQueryWrapper = new QueryWrapper<>();
        if (null != industryVO.getKeywords() && industryVO.getKeywords().trim().length() > 0) {
            industryQueryWrapper.like("name", industryVO.getKeywords()).or().like("type", industryVO.getKeywords());
        }
        industryQueryWrapper.eq("develop_id", developer.getId());
        // 允许进行平台个性化配置
        // industryQueryWrapper.notIn("type", IndustryEnum.SYS.name().toLowerCase());
        Page<Industry> page = industryService.page(industryVO.getPage(), industryQueryWrapper);
        List<Industry> records = page.getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            for (Industry industry : records) {
                industry.setDevelopEntName(developerDb.getEnterpriseName());
            }
        }
        return ResultJson.success(page);
    }

}
